#!/bin/bash
# (C) Copyright 2012,2013 Continuent, Inc - Released under the New BSD License
# Version 1.0.6 - 2013-08-11

cookbook_dir=$(dirname $0)
cd "$cookbook_dir/../"

if [ ! -f $cookbook_dir/BOOTSTRAP.sh ]
then
    echo "$cookbook_dir/BOOTSTRAP.sh not found"
    exit 1
fi
. $cookbook_dir/BOOTSTRAP.sh NODES_FAN_IN.sh
. $cookbook_dir/utilities.sh

check_installed
check_for_deprecated_installer
# $cookbook_dir/clear_cluster

echo "installing FAN-IN" >$INSTALL_LOG
date >> $INSTALL_LOG

if [ -n "$DRYRUN" ]
then
    echo "WARNING: DRYRUN is deprecated - Future versions of Tungsten Cookbook will only support tpm-based installations"
    [ -z "$VERBOSE" ] && VERBOSE=1
fi 

COMMAND_SEQUENCE=1

# install fan in
if [ -n "$USE_TPM" ]
then
    check_security
    MEMBERS=${MASTERS[*]}
    SERVICE_INDEX=0
    INSTALL_COMMAND="./tools/tpm install fan_in \
        --reset \
        --log=timestamp \
        --replication-port=$DATABASE_PORT \
        --replication-user=$DATABASE_USER \
        --replication-password=$DATABASE_PASSWORD \
        --datasource-log-directory=$BINLOG_DIRECTORY \
        --home-directory=$TUNGSTEN_BASE \
        --datasource-mysql-conf=$MY_CNF \
        --datasource-log-directory=$BINLOG_DIRECTORY \
        -a --datasource-boot-script=$MYSQL_BOOT_SCRIPT \
        --repl-disable-relay-logs=true \
        --rmi-port=$RMI_PORT \
        --thl-port=$THL_PORT \
        --masters=$MASTERS_LIST \
        --slaves=$FAN_IN_SLAVE \
        --master-services=$MASTER_SERVICES_LIST \
        --topology=fan-in \
        $MORE_OPTIONS $MORE_TPM_OPTIONS $SECURITY_OPTIONS --$START_OPTION"

    run_command $INSTALL_COMMAND 
else
    INDEX=0

    #Install the Master replicators on all the hosts

    #for NODE in ${MASTERS[*]} $FAN_IN_SLAVE
    for NODE in ${ALL_NODES[*]}
    do
       
      INSTALL_COMMAND="./tools/tungsten-installer \
        --master-slave \
        --master-host=$NODE \
        --datasource-port=$DATABASE_PORT \
        --datasource-user=$DATABASE_USER \
        --datasource-password=$DATABASE_PASSWORD \
        --datasource-log-directory=$BINLOG_DIRECTORY \
        --service-name=${MM_SERVICES[$INDEX]} \
        --home-directory=$TUNGSTEN_BASE \
        --cluster-hosts=$NODE \
        --datasource-mysql-conf=$MY_CNF \
        --datasource-log-directory=$BINLOG_DIRECTORY \
        --disable-relay-logs \
        -a --datasource-boot-script=$MYSQL_BOOT_SCRIPT \
        --rmi-port=$RMI_PORT \
        --thl-port=$THL_PORT \
        $MORE_OPTINS --$START_OPTION"     

        echo "## $COMMAND_SEQUENCE (host: $NODE)" >> $INSTALL_LOG
        echo $INSTALL_COMMAND | perl -pe 's/--/\\\n\t--/g' >> $INSTALL_LOG
        if [ -n "$VERBOSE" ]
        then
            echo "## $COMMAND_SEQUENCE (host: $NODE)"
            echo $INSTALL_COMMAND | perl -pe 's/--/\\\n\t--/g'
        fi
        COMMAND_SEQUENCE=$(($COMMAND_SEQUENCE+1))
        if [ -z "$DRYRUN" ]
        then
            $INSTALL_COMMAND

            if [ "$?" != "0"  ]
            then
                exit 1
            fi
        fi
        INDEX=$(($INDEX+1))
    done

    FAN_IN_DS=`echo $FAN_IN_SLAVE|perl -lpe's/\W/_/g'`

    TUNGSTEN_TOOLS=$TUNGSTEN_BASE/tungsten/tools
    COMMON_OPTIONS="--advanced -C -q 
        --local-service-name=$FAN_IN_LOCAL_SERVICE
        --role=slave 
        --service-type=remote 
        --log-slave-updates=true
        --datasource=$FAN_IN_DS"

    INDEX=0
    # set -x

    #Install all the Slave replicators from the Masters on the Fan-In Slave

    for REMOTE_MASTER in ${MASTERS[*]}
    do
        if [ "$FAN_IN_LOCAL_SERVICE" == "${MM_SERVICES[$INDEX]}" ]
        then
            INDEX=$(($INDEX+1))
        fi
        INSTALL_COMMAND="$TUNGSTEN_TOOLS/configure-service \
            --host=$FAN_IN_SLAVE \
            ${COMMON_OPTIONS} \
            --master-thl-host=$REMOTE_MASTER \
            --master-thl-port=$THL_PORT \
            --svc-$START_OPTION ${MM_SERVICES[$INDEX]}"

        echo "## $COMMAND_SEQUENCE (host: $FAN_IN_SLAVE)" >> $INSTALL_LOG
        echo $INSTALL_COMMAND | perl -pe 's/--/\n\t--/g' >> $INSTALL_LOG
        if [ -n "$VERBOSE" ]
        then
            echo "## $COMMAND_SEQUENCE (host: $FAN_IN_SLAVE)"
            echo $INSTALL_COMMAND | perl -pe 's/--/\\\n\t--/g'
        fi
        COMMAND_SEQUENCE=$(($COMMAND_SEQUENCE+1))
        if [ -z "$DRYRUN" ]
        then
            $INSTALL_COMMAND
            if [ "$?" != "0"  ]
            then
                exit 1
            fi
        fi
        INDEX=$(($INDEX+1))
    done
fi
#set +x

if  [ -n "$DRYRUN" ]
then
    echo "## $COMMAND_SEQUENCE (host: $(hostname)"
    echo "echo 'fan_in' > $CURRENT_TOPOLOGY"
    exit
fi

echo "fan_in" > $CURRENT_TOPOLOGY
$cookbook_dir/show_cluster 
post_installation

